<?php
namespace Home\Service;

/*use Cli\Service\CustomerCountServiceModel;*/
/*use Home\Model\DailyDataModel;*/
/*use Home\Model\GroupCustomerStatisticsModel;
use Home\Model\UserCustomerStatisticsModel;
use Common\Lib\GetWeek;
use Common\Lib\GetMonth;*/

class RecruitChannelsGather {

    private $start =   '';
    private $end   =   '';
    private $order = 'id asc';

    private $today = '';


    public function __construct(){
        $this->today = Date('Y-m-d');
    }



    private function getFields(){
        return array('wb_interviewer_num',  'ganji_interviewer_num', 'zhilian_interviewer_num', 'qiancheng_interviewer_num', 'jljh_interviewer_num', 'boss_interviewer_num', 'campus_interviewer_num', 'introduce_interviewer_num', 'other_interviewer_num', 'interviewer_total', 'wb_entry_num', 'ganji_entry_num', 'zhilian_entry_num', 'qiancheng_entry_num', 'jljh_entry_num', 'boss_entry_num', 'campus_entry_num', 'introduce_entry_num', 'other_entry_num', 'entry_total');
    }


    private function getSqlFields(){
        $field = $this->getFields();
        $arr = array();
        foreach ($field as $key => $value) {
            $arr[] = "sum(`". $value . "`) as ".$value;
        }
        return implode(",", $arr);
    }
    public function reSort($re){
        $columen = array_column($re, $this->field);
        array_multisort($columen, $this->sort , SORT_NUMERIC, $re);
        return $re;
    }

/*    public function getDepartment($start,$end,$department_id,$role_id,$sort_field,$sort_order){
        $this->start =$start;
        $this->end = $end;
         $department_id = $department_id;
        $role_id = $role_id;
        switch($role_id){
            case 16:
              $list  = M('recruit_channels')->field($this->getSqlFields().", department_id as id, department_name as name")
                                                            ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'department_id'=>$department_id))
                                                            ->order($sort_field.' '.$sort_order)
                                                            ->select(); 
            break;
            default :
              $list  = M('recruit_channels')->field($this->getSqlFields().", department_id as id, department_name as name")
                                                            ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('department_id')
                                                            ->order($sort_field.' '.$sort_order)
                                                            ->select();     
        }                                                                                  
        return $this->reSort($list);
    }*/

    /*重新更改 开始*/
       public function setDate($start, $end){
        $this->start = $start;
        $this->end   = $end;
        return $this;
    }

      public function setOrder($order){
        $this->order = $order;
        $fields  = explode(' ', $this->order);
        $this->field = $fields[0];
        $sortMap = array('asc'=>SORT_ASC, 'desc'=>SORT_DESC );
        $this->sort = $sortMap[$fields[1]];
        return $this;
    }

    public function getDepartment(){
            $info_sources = $this->getInfoSources();
            $entry_sources = $this->getEntrySources();
           //$departments = M("Department_basic")->field("id,name")->order("id asc")->select();
            //获取所有CRM部门 (二维)
            $getalldepartments_url = "Department/getAllDepartments";
            $alldepartments = json_decode(http_get($getalldepartments_url),true);
            $departments = $alldepartments["list"];
            $list  = M('recruit_info')->field("department_id,count(id) as count,info_sources")
                                                            ->where(array('interviewer_date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('info_sources,department_id')
                                                            ->select();
            //按部门统计
            $interviewer_total  = M('recruit_info')->field("department_id,count(id) as count,info_sources")
                                                            ->where(array('interviewer_date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('department_id')
                                                            ->select();

            //按入职统计
            $entry_total  = M('recruit_info')->field("department_id,count(id) as count,info_sources")
                                                            ->where(array('entry_date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'status'=>array('in', '1,2')))
                                                            ->group('department_id')
                                                            ->select();
            $list2  = M('recruit_info')->field("department_id,count(id) as count,info_sources,status")
                                                            ->where(array('entry_date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'status'=>array('in', '1,2')))
                                                            ->group('info_sources,department_id')
                                                            ->select();
          /*对各个招聘网站面试的人数进行统计 开始*/
        $new_list = $this->getEachInfoSources($departments,$list,$info_sources,$parameter1 = 'department_id');//同一个部门的数据组成一个数组  
        $new_list =$this->InsertInterviewerTotal($new_list,$interviewer_total,$dep_user_id = "department_id",$total="interviewer_total");
        $new_list = $this->getAllInfoSources($new_list,$info_sources,$total="interviewer_total");  
        $new_list = $this->getAllDepInfoSources($departments,$new_list,$info_sources,$total = "interviewer_total"); 
        $new_list = $this->judgeEmpty($new_list,$info_sources,$total="interviewer_total");
          
        /*对各个招聘网站面试的人数进行统计 结束*/
        ////////////////////////////////////////
        /*对各个招聘网站入职的人数进行统计 开始*/
        $new_list2 = $this->getEachInfoSources($departments,$list2,$entry_sources,$parameter1 = 'department_id');//同一个部门的数据组成一个数组
        $new_list2 = $this->InsertInterviewerTotal($new_list2,$entry_total,$dep_user_id='department_id',$total='entry_total');
        $new_list2 = $this->getAllInfoSources($new_list2,$entry_sources,$total="entry_total");
        $new_list2 = $this->getAllDepInfoSources($departments,$new_list2,$entry_sources,$total = "entry_total"); 
        
        $new_list2 = $this->judgeEmpty($new_list2,$entry_sources,$total="entry_total");
        $double_list = $this->mergeList($new_list,$new_list2,$info_sources,$entry_sources,$total1="interviewer_total",$total2 = "entry_total");
        // $double_list = $this->getNewOrder($double_list);
         /*对各个招聘网站入职的人数进行统计 结束*/    
   
        return $this->reSort($double_list);
    }
    public function getUsers($department_id){
            $info_sources = $this->getInfoSources();
            $entry_sources = $this->getEntrySources();
            //$users = M("user_info")->getfield("user_id,realname");
            $users = M("user_info")->field("user_id as id,realname as name")->order("user_id asc")->select();
            $dep_users = M("user_info")->field("user_id as id,realname as name") -> where(array("department_id"=>$department_id))->order("user_id asc")->select(); 
            //面试统计
            $list  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.info_sources,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.department_id'=>$department_id ))
                                                            ->where(array('interviewer_date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('recruit_info.info_sources,recruit_info.user_id')
                                                            ->select();
             //按个人统计总面试数
            $interviewer_total  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.department_id'=>$department_id ))
                                                            ->where(array('interviewer_date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('recruit_info.user_id')
                                                            ->select();
            //入职统计
            $list2  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.info_sources,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.department_id'=>$department_id ))
                                                            ->where(array('recruit_info.entry_date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'status'=>array('in', '1,2')))
                                                            ->group('recruit_info.info_sources,recruit_info.user_id')
                                                            ->select();
            //按个人统计总入职数
            $entry_total  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.department_id'=>$department_id ))
                                                            ->where(array('recruit_info.entry_date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'status'=>array('in', '1,2')))
                                                            ->group('recruit_info.user_id')
                                                            ->select();
          
            $new_list = $this->getEachInfoSources($users,$list,$info_sources,$parameter1 = 'user_id');//同一个用户的数据组成一个数组
            $new_list =$this->InsertInterviewerTotal($new_list,$interviewer_total,$dep_user_id = "user_id",$total="interviewer_total");
            $new_list = $this->getAllInfoSources($new_list,$info_sources,$total="interviewer_total");
            $new_list = $this->getAllDepInfoSources($dep_users,$new_list,$info_sources,$total = "interviewer_total");//查询该部门下的所有人事专员的数据
            $new_list = $this->judgeEmpty($new_list,$info_sources,$total="interviewer_total");
            $new_list2 = $this->getEachInfoSources($users,$list2,$entry_sources,$parameter1 = 'user_id');//同一个用户的数据组成一个数组
            $new_list2 =$this->InsertInterviewerTotal($new_list2,$entry_total,$dep_user_id = "user_id",$total="entry_total");
            $new_list2 = $this->getAllInfoSources($new_list2,$entry_sources,$total="entry_total");//不存在的信息来源设置成零
            $new_list2 = $this->getAllDepInfoSources($dep_users,$new_list2,$entry_sources,$total = "entry_total");//查询该部门下的所有人事专员的数据
            $new_list2 = $this->judgeEmpty($new_list2,$entry_sources,$total="entry_total");
            $double_list = $this->mergeList($new_list,$new_list2,$info_sources,$entry_sources,$total1="interviewer_total",$total2 = "entry_total");
            //$double_list = $this->getNewOrder($double_list);
            
           /* $new_double_list = $double_list;
            $new_double_list["count"] = count($double_list);*/
             return $this->reSort($double_list);
    }

    public function getSingleUserData($user_id){
           $info_sources = $this->getInfoSources();
           $entry_sources = $this->getEntrySources();
           //面试统计
            $list  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.info_sources,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.user_id'=>$user_id ))
                                                            ->where(array('interviewer_date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('recruit_info.info_sources')
                                                            ->select();
               //按个人统计总面试数
            $interviewer_total  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.user_id'=>$user_id ))
                                                            ->where(array('interviewer_date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->select();
            //入职统计
            $list2  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.info_sources,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.user_id'=>$user_id ))
                                                            ->where(array('recruit_info.entry_date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'status'=>array('in', '1,2')))
                                                            ->group('recruit_info.info_sources')
                                                            ->select();
            //统计总入职数
            $entry_total  = M('recruit_info')->join("user_info as ui on recruit_info.user_id=ui.user_id")
            ->field("recruit_info.department_id,count(recruit_info.id) as count,recruit_info.user_id ,realname")
                                                            ->where(array('recruit_info.user_id'=>$user_id ))
                                                            ->where(array('recruit_info.entry_date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'status'=>array('in', '1,2')))
                                                            ->group('recruit_info.user_id')
                                                            ->select();
            $new_list = $this->getSingleEachInfoSources($list,$info_sources);//同一个用户的数据组成一个数组
            $new_list = $this->InsertInterviewerTotal($new_list,$interviewer_total,'user_id',$total='interviewer_total');
            $new_list = $this->getAllInfoSources($new_list,$info_sources,$total="interviewer_total");
            $new_list = $this->judgeSingleEmpty($new_list,$info_sources,$total="interviewer_total",$user_id);
            $new_list2 = $this->getSingleEachInfoSources($list2,$entry_sources);//同一个用户的数据组成一个数组
            $new_list2 = $this->InsertInterviewerTotal($new_list2,$entry_total,'user_id',$total='entry_total');
            $new_list2 = $this->getAllInfoSources($new_list2,$entry_sources,$total="entry_total");
            $new_list2 = $this->judgeSingleEmpty($new_list2,$entry_sources,$total="entry_total",$user_id);
            $double_list = $this->mergeList($new_list,$new_list2,$info_sources,$entry_sources,$total1="interviewer_total",$total2 = "entry_total");
            //$double_list = $this->getNewOrder($double_list);
             return $this->reSort($double_list);
    }


    private function getInfoSources(){
            $info_sources = array("1"=>"campus_interviewer_num","2"=>"wb_interviewer_num", "3"=>"ganji_interviewer_num", "4"=>"zhilian_interviewer_num", "5"=>"qiancheng_interviewer_num", "6"=>"jljh_interviewer_num", "7"=>"boss_interviewer_num",  "8"=>"introduce_interviewer_num", "9"=>"other_interviewer_num");
            return $info_sources;

    }
    private function getEntrySources(){
        $entry_sources = array("1"=>"campus_entry_num","2"=>"wb_entry_num", "3"=>"ganji_entry_num", "4"=>"zhilian_entry_num", "5"=>"qiancheng_entry_num", "6"=>"jljh_entry_num", "7"=>"boss_entry_num", "8"=>"introduce_entry_num", "9"=>"other_entry_num");
        return $entry_sources;
    }
    //同一个用户的数据组成一个数组
    private function getEachInfoSources($arr,$list,$info_sources,$parameter1){
            $new_list = array();
            foreach($arr as $key=>$value){
                foreach($list as $k => $v){
                    if($v[$parameter1] == $value['id']){
                        $new_list[$key][$info_sources[$v['info_sources']]] = $v['count']; 
                        $new_list[$key]['id'] = $v[$parameter1];
                        $new_list[$key]['name'] = $value['name'];   
                    } 
                }   
            }
        return $new_list;

    }
    private function InsertInterviewerTotal($new_list,$interviewer_entry_total,$dep_user_id,$total){
         foreach($new_list as $key =>$value){
            foreach($interviewer_entry_total as $k=>$v){
                if($value['id'] == $v[$dep_user_id]){
                    $new_list[$key][$total] = $v['count'];
                }
            }
        }
        return $new_list;
    }
    //将数组中不存在的信息来源中的数据设置成零
    private function getAllInfoSources($new_list,$info_sources,$total){
         array_push($info_sources,$total);
         foreach($info_sources as $k=>$v){
            foreach($new_list as $kk=>$vv){
                if(empty($vv[$v])){
                    $new_list[$kk][$v]=0;
                }
            }
        }
        return $new_list;
    }
    //获取所有部门或者所有人的面试或入职数据
    private function getAllDepInfoSources($departments,$new_list,$info_sources,$total){
        array_push($info_sources,$total);
        foreach($departments as $key=>$value){
            foreach($new_list as $k=>$v){
                foreach($v as $kk=>$vv){
                    if($value['id'] == $v['id'] ){
                    $departments[$key][$kk] = $vv;
                    }
                }     
            }
        }
        return $departments;
    }
  
     //将为空的数据设置成0
    private function judgeEmpty($new_list,$info_sources,$total){
        array_push($info_sources,$total);
        foreach($new_list as $key=>$value){
            foreach($info_sources as $k=>$v){
                if(empty($value[$v])){
                    $new_list[$key][$v] = 0;
                }
            }
        }
        return $new_list;
    }
    //合并$new_list和$new_list2
    private function mergeList($new_list,$new_list2,$info_sources,$entry_sources,$total1,$total2){
        if(!empty($new_list)&&empty($new_list2)){
            array_push($entry_sources,$total2);
            $sources = $entry_sources;
            $new_list =$this->mergeListBranch1($new_list,$sources);
        }else if(!empty($new_list2)&&empty($new_list)){
            $new_list = $new_list2;
            $new_list2 = $new_list;
            array_push($info_sources,$total1);
            $sources = $info_sources;
            $new_list = $this->mergeListBranch1($new_list,$sources);
        }else if(!empty($new_list)&&!empty($new_list2)){
            $new_list = $this->mergeListBranch2($new_list,$new_list2);
        }
        return $new_list;
    }
    private function mergeListBranch1($new_list,$sources){
          foreach($new_list as $key=>$value){
                foreach($sources as $k=>$v){
                    $new_list[$key][$v] = 0;
                }
            }
        return $new_list;

    }
    private function mergeListBranch2($new_list,$new_list2){
            foreach($new_list as $key=>$value){
                foreach($new_list2 as $k=>$v){
                    foreach($v as $kk=>$vv){
                        if($value['id'] == $v['id']){
                            $new_list[$key][$kk] = $vv;
                        }
                    }
                }
            }
            return $new_list;
    }
  
    //将数组从零开始排序
    private function getNewOrder($arr){
        $aa = 0;
        $new_arr = array();
        foreach($arr as $kk=>$vv){
            foreach($vv as $kkk=>$vvv){
                $new_arr[$aa][$kkk] = $vvv;
            }   
            $aa++;
        }
        return $new_arr;
    }
    //将同一用户的数据放在一个数组
    private function getSingleEachInfoSources($list,$info_sources){
        $new_list  =array();
        foreach($list as $key => $value){
            $new_list['0']['id'] = $value['user_id'];
            $new_list['0']['name'] = $value['realname'];
            $new_list['0'][$info_sources[$value['info_sources']]] = $value['count'];
        }
        return $new_list;
    }
    private function judgeSingleEmpty($new_list,$info_sources,$total,$user_id){
        $user = M("User_info")->field("user_id as id ,realname as name")->where(array('user_id'=>$user_id))->select();
        array_push($info_sources,$total);
        if(empty($new_list)){
            foreach($user as $key=>$value){
                foreach($info_sources as $k=>$v){
                    if(empty($value[$v])){
                        $new_list['0'][$v] = 0;
                    }
                } 
                $new_list["0"]["id"] = $value["id"];
                $new_list["0"]["name"] = $value["name"];
            }  
        }
        return $new_list;
    }
    /*重新更改 结束*/




    
}